clear
set mem 300m

capture log close
log using "$dataloc/log/wdi_weo_metrics", text replace
set more off

/* --------------------------------------------------------------- *
    Author: Ed Al-Hussainy
    Description:  Process WDI and WEO data for Financial Structure Database
 * --------------------------------------------------------------- */
 
forvalues i = 1/10 { 
    tempfile tmp`i' 
} 
 
/* --------------------------------------------------------------- *
   WDI
 * --------------------------------------------------------------- */
 
insheet using "$dataloc/Data/WDI/$dataver/wdi1.csv", names clear

rename countrycode cncode

rename gdpconstant2000us wdi_gdp_usd_2k
label var wdi_gdp_usd_2k "WDI GDP (2000 USD)"

rename gdpcurrentlcu wdi_gdp_lcu
label var wdi_gdp_lcu "WDI GDP (current LCU)"

rename gdpcurrentus wdi_gdp_usd_cur
label var wdi_gdp_usd_cur "WDI GDP (current USD)"

rename gdppppconstant2005international wdi_gdp_ppp
label var wdi_gdp_ppp "WDI GDP PPP (2005 intl USD)"

rename consumerpriceindex2005100 wdi_cpi
label var wdi_cpi "WDI CPI (2005=100)"

rename officialexchangeratelcuperusperi wdi_xrate
label var wdi_xrate "WDI EXCHANGE RATE (LCU/USD)"

rename realeffectiveexchangerateindex20 wdi_reer
label var wdi_reer "WDI REER"

rename tradeofgdp wdi_trade_gdp
label var wdi_trade_gdp "TRADE / GDP"

rename agedependencyratioofworkingagepo wdi_agedep
label var wdi_agedep "AGE DEPENDENCY RATIO"

rename populationtotal wdi_pop
replace wdi_pop = wdi_pop / 10^6
label var wdi_pop "POPULATION (MIL)"

rename urbanpopulationoftotal wdi_urbanpop
label var wdi_urbanpop "URBAN POPULATION (% OF TOTAL)"

rename interestratespreadlendingratemin wdi_spread
label var wdi_spread "INTEREST RATE SPREAD"

rename domesticcreditprovidedbybankings wdi_bankcred
label var wdi_bankcred "BANK CREDIT / GDP"

rename domesticcredittoprivatesectorofg wdi_privcred
label var wdi_privcred "PRIVATE SECTOR CREDIT / GDP"

rename stockstradedturnoverratio S01WDI0
label var S01WDI0 "STOCK MKT TURNOVER RATIO"

rename marketcapitalizationoflistedcomp S02WDI0
label var S02WDI0 "STOCK MKT CAPITALIZATION / GDP"

rename stockstradedtotalvalueofgdp S03WDI0
label var S03WDI0 "STOCK MKT TOTAL VALUE TRADED / GDP"

rename workersremittancesandcompensatio S04WDI0
label var S04WDI0 "REMITTANCES / GDP"

foreach var of varlist S0* {
   replace `var'=`var'/100
}

keep cncode year wdi* S*

tempfile wdi
save `wdi'

/* --------------------------------------------------------------- *
   WEO: GDP data for latest year
 * --------------------------------------------------------------- */
 
insheet using "$dataloc/Data/WEO/$dataver/WEOOct.csv", names clear

foreach i of var v* {
   local j "`:var label `i''"
   rename `i' y`j'
}   

* CPI, national GDP in current LCU and current USD
keep if inlist(weosubjectcode,"PCPI", "NGDP", "NGDPD")

rename iso cncode

keep cncode weosubjectcode y*

foreach i of var y* {
   replace `i' = subinstr(`i',",","",.)
   replace `i' = subinstr(`i',"--","",.)
   replace `i' = "" if `i' == "n/a"
   destring `i', replace
}   

reshape long y, i(cncode weosubjectcode) j(year)
reshape wide y, i(cncode year) j(weosubjectcode) string

rename yNGDP weo_gdp_lcu
replace weo_gdp_lcu = weo_gdp_lcu * 10^9 

rename yNGDPD weo_gdp_usd_cur
replace weo_gdp_usd_cur = weo_gdp_usd * 10^9 

rename  yPCPI weo_cpi

* merge, rescale CPI to 2005 = 100

merge cncode year using `wdi', sort
drop _merge

preserve
   keep if year == 2005
   rename weo_cpi weo_cpi_05
   rename wdi_cpi wdi_cpi_05   
   drop year
   sort cncode
   save `tmp1'
restore   

sort cncode

merge cncode using `tmp1', uniqusing
drop _merge
replace weo_cpi = (weo_cpi / weo_cpi_05) * 100
replace wdi_cpi = (wdi_cpi / wdi_cpi_05) * 100
drop weo_cpi_05 wdi_cpi_05

label var weo_gdp_lcu "WEO GDP (current LCU)"
label var weo_gdp_usd_cur "WEO GDP (current USD)"
label var weo_cpi "WEO CPI (2005=100)"

/* --------------------------------------------------------------- *
    save results
 * --------------------------------------------------------------- */

drop if year < $startyear - 1 
drop if year > $endyear

isid cncode year
sort cncode year

compress 
 
label data "WDI & WEO Metrics (Version: $dataver)"
save "$dataloc/Output/WDI_WEOmetrics", replace


/* end of code */
clear
log close
exit
